package com.ccsu.mymarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ccsu.mymarket.entity.User;
import com.ccsu.mymarket.entity.message.Meta;
import com.ccsu.mymarket.entity.message.Result;
import com.ccsu.mymarket.jwt.UserLoginToken;
import com.ccsu.mymarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@UserLoginToken
@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    public UserController(@Autowired UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/all")
//    管理员查看所有普通用户
    public Result getAllUser(){
        List<User> userList=userService.list(new LambdaQueryWrapper<User>()
                .eq(User::getRole,1));
        if (userList!=null)
            return new Result(new Meta(200, "查询成功！"), userList);
        return new Result(new Meta(201, "暂无数据！"), null);
    }

    @PutMapping("/update/seller")
//    根据前台传入的 shopId-店铺id 修改 shopType-经营类型 或 phone-电话号码
    public Result updateSellerInfo(@RequestBody Map data){
        if (data == null)
            return new Result(new Meta(403, "请输入有效信息！"), null);

        Integer shopId= (Integer) data.get("shopId");

        if (shopId==null)
            return new Result(new Meta(403, "店铺id不能为空！"), null);

        Integer shopType= (Integer) data.get("shopType");
        String phone= (String) data.get("phone");
        if (userService.updateSellerInfo(shopId,shopType,phone))
            return new Result(new Meta(200, "修改成功！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }

    @GetMapping("/search/key")
//    输入用户昵称(关键字)进行模糊查询
    public Result searchUserByName(@RequestParam String key){
        List<User> userList=userService.list(new LambdaQueryWrapper<User>()
                .like(User::getAccName,key));
        if (userList!=null)
            return new Result(new Meta(200, "查询成功！"), userList);
        return new Result(new Meta(201, "暂无数据！"), null);
    }

    @GetMapping("/search/{id}")
//    传入id查询用户基本信息
    public Result searchUserByID(@PathVariable Integer id){
        User user=userService.getById(id);
        if (user.getId() != null )
            return new Result(new Meta(200, "查询成功！"), user);
        return new Result(new Meta(201, "暂无数据！"), null);
    }

    @DeleteMapping("/delete/{id}")
//    传入用户id进行删除
    public Result deleteUserById(@PathVariable Integer id){
        if (userService.removeById(id))
            return new Result(new Meta(200, "成功删除用户！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }

//    修改用户信息,根据需求选择字段,字段名与User类相同
     @PutMapping("/update/user")
    public Result updateUserInfo(@RequestBody User user){
        if(userService.updateUserInfo(user))
            return new Result(new Meta(200, "修改成功！"), null);
         return new Result(new Meta(500, "操作失败！"), null);
     }

    @PutMapping("/update/admin")
    public Result updateUserByAdmin(@RequestBody User user){
        if(userService.updateUserByAdmin(user))
            return new Result(new Meta(200, "修改成功！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }
}

/**
    @PutMapping("/refresh/{id}")
//    服务器提示用户token过期时，调用此接口，返回新token
    public Result refreshToken(@PathVariable Integer id) throws MyException {
        String tokenNew= JWTUtils.buildJWT(id);
        if (tokenNew == null)
            throw new MyException("token刷新失败！");
        return new Result(new Meta(200, "token刷新成功！"), tokenNew);
    }
*/